package org.epidroid.epidroid.chronos.ui;

import org.epidroid.epidroid.chronos.engine.Chronos;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.format.Time;
import android.util.Log;
import android.widget.TextView;

public class ChronosCompactViewActivity extends Activity {

	private ViewPager _pager = null;
	private MyCompactViewAdapter _adapter = null;
	private int _focusedPage = 0;

	private Time _leftTime = null;
	private Time _rightTime = null;
	private Time _time = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//setContentView(R.layout.chronos_compact_view);
		//_pager = (ViewPager) findViewById(R.id.chronos_compact_view_pager);

		_adapter = new MyCompactViewAdapter(this);

		_pager.setAdapter(_adapter);

		_time = new Time();
		_time.set(System.currentTimeMillis());

		_leftTime = new Time(_time);
		_leftTime.monthDay--;
		_leftTime.normalize(true);

		_rightTime = new Time(_time);
		_rightTime.monthDay++;
		_rightTime.normalize(true);

		debugTime(_time);
		debugTime(_leftTime);
		debugTime(_rightTime);

		initPager();
	}

	private void debugTime(Time time) {
		Log.d(Chronos.LOGCAT, time.format2445());
	}

	private void pagerSetTime(int position, Time time) {
		TextView txt = (TextView) _pager.getChildAt(position);
		if (txt == null) {
			Log.d(Chronos.LOGCAT, "fail");
			return;
		}

		txt.setText(time.format3339(true));
	}

	private void initPager() {

		_pager.setCurrentItem(1);
		pagerSetTime(0, _leftTime);
		pagerSetTime(1, _time);
		pagerSetTime(2, _rightTime);

		_pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				_focusedPage = position;
			}

			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int state) {
				if (state == ViewPager.SCROLL_STATE_IDLE) {

					/*
					 * Scroll left
					 */
					if (_focusedPage == 0) {
						final Time time = new Time(_leftTime);
						time.monthDay--;
						time.normalize(true);

						Time tmp = _leftTime;
						_leftTime = time;
						_rightTime = _time;
						_time = tmp;

						pagerSetTime(0, _rightTime);
						pagerSetTime(1, _time);
						pagerSetTime(2, _leftTime);
					}
					/*
					 * Scroll right
					 */
					else if (_focusedPage == 2) {

						Log.d(Chronos.LOGCAT, "scroll right:");
						debugTime(_rightTime);

						final Time time = new Time(_rightTime);
						time.monthDay++;
						time.normalize(true);

						Time tmp = _rightTime;
						_rightTime = time;
						_leftTime = _time;
						_time = tmp;

						pagerSetTime(0, _rightTime);
						pagerSetTime(1, _time);
						pagerSetTime(2, _leftTime);
					}

					debugTime(_time);

					_pager.setCurrentItem(1, false);

				}
			}
		});
	}

}
